﻿using ComLib.ComFun;
using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace PrintExcelSetting
{
    public partial class FormPaperPrintingSetting : MetroForm
    {
        private string _directory = "";
        public string AssemblyDirectory
        {
            get
            {
                if(string.IsNullOrEmpty(_directory))
                {
                    string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                    _directory = directory.Substring(6);
                }
                return _directory;
            }
        }

        private PaperPrinterOption _printer = new PaperPrinterOption();
        public PaperPrinterOption Printer
        {
            get { return _printer; }
        }

        public FormPaperPrintingSetting()
        {
            InitializeComponent();
        }

        private void LoadConfig()
        {
            if (!File.Exists(_printer.ConfigFilePath))
            {
                _printer.Save();
            }
            _printer.Load();
        }

        private void FormPaperPrintingSetting_Load(object sender, EventArgs e)
        {
            LoadConfig();
            InitCtrls();
        }

        private void InitCtrls()
        {
            if (PrinterService.CreatePrinterService())
            {
                int selectedIndex = 0;
                List<string> printers = PrinterService.Printers;
                for (int i = 0; i < printers.Count; i++)
                {
                    metroComboBoxPrinter.Items.Add(printers[i]);
                    if (printers[i].Equals(_printer.Printer))
                    {
                        selectedIndex = i;
                    }
                }
                metroComboBoxPrinter.SelectedIndex = selectedIndex;
            }
            textBoxDeviceName.Text = _printer.DeviceName;
            textBoxDeviceNo.Text = _printer.DeviceNo;   
            numericUpDownTimeout.Value = _printer.Timeout;
            metroTogglePrintHeadImage.Checked = _printer.IsPrintPerson;
        }

        private bool UpdateData()
        {
            if(string.IsNullOrEmpty(textBoxDeviceNo.Text)
                || string.IsNullOrEmpty(textBoxDeviceName.Text))
            {
                MessageBox.Show("配置项不能为空!");
                return false;
            }            
        
            _printer.DeviceName = textBoxDeviceName.Text;
            _printer.DeviceNo = textBoxDeviceNo.Text;
            _printer.IsPrintPerson = metroTogglePrintHeadImage.Checked;
            _printer.Timeout = (int)numericUpDownTimeout.Value;
            if (metroComboBoxPrinter.SelectedIndex >= 0)
            {
                _printer.Printer = metroComboBoxPrinter.SelectedItem.ToString();
            }
            _printer.Save();
            return true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if(UpdateData())
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }            
        }
    }
}
